package com.sendpo.zjx.sendpo.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.BuildConfig;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.UserDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.Callback.CommonCallback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.shaohui.bottomdialog.BottomDialog;

public class BussStoreShenfenRenzhenActivity
        extends AppCompatActivity {
    public final int REQUEST_CAMERA = 104;
    public final int REQUEST_CAMERA1 = 107;
    public final int REQUEST_CAMERA2 = 108;
    public final int REQUEST_CAMERA3 = 201;
    public final int REQUEST_CROP = 105;
    public final int REQUEST_CROP1 = 106;
    public final int REQUEST_CROP2 = 109;
    public final int REQUEST_CROP3 = 200;
    public final int REQUEST_IMAGECOUNT = 1031;
    public final int REQUEST_IMAGECOUNT1 = 1032;
    public final int REQUEST_IMAGECOUNT2 = 1033;
    public final int REQUEST_IMAGECOUNT3 = 1050;
    BottomDialog bottomDialog;
    BottomDialog bottomDialog1;
    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.real_name_tv)
    TextView realNameTv;
    @BindView(R.id.realname_edit)
    EditText realnameEdit;
    @BindView(R.id.number_name_tv)
    TextView numberNameTv;
    @BindView(R.id.card_edit)
    EditText cardEdit;
    @BindView(R.id.shenfen_title)
    TextView shenfenTitle;
    @BindView(R.id.zhengmian_iv)
    ImageView zhengmianIv;
    @BindView(R.id.zhengmian_bt)
    RelativeLayout zhengmianBt;
    @BindView(R.id.fanmian_iv)
    ImageView fanmianIv;
    @BindView(R.id.fanmian_bt)
    RelativeLayout fanmianBt;
    @BindView(R.id.shouchi_iv)
    ImageView shouchiIv;
    @BindView(R.id.shouchi_bt)
    RelativeLayout shouchiBt;
    @BindView(R.id.yinyezhizhao_iv)
    ImageView yinyezhizhaoIv;
    @BindView(R.id.yinyezhizhao_bt)
    RelativeLayout yinyezhizhaoBt;
    @BindView(R.id.submitbt)
    Button submitbt;
    private BottomDialog bottomDialog2 = null;
    private BottomDialog bottomDialog3 = null;

    RelativeLayout head;
    public Dialog loadingdialog = null;
    private String merchantsAddress = "";
    private String merchantsArea = "";
    private String merchantsCity = "";
    private String merchantsIndustry = "";
    private String merchantsLatitude = "";
    private String merchantsLongitude = "";
    private String merchantsName = "";
    private String merchantsPhone = "";
    private String merchantsProvince = "";

    File temp_cmera = null;
    private String temppath1 = "";
    private String temppath2 = "";
    private String temppath3 = "";
    private String temppath4 = "";


    private void initComponent() {
        bottomDialog = BottomDialog.create(getSupportFragmentManager());
        bottomDialog.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog.dismiss();
                    }
                });
                View camera = v.findViewById(R.id.camera);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        temp_cmera = new File(getExternalCacheDir().getAbsolutePath()+"/" + UUID.randomUUID().toString().substring(0, 10) + ".jpg");
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra("return-data", true);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                        } else {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                        }
                        startActivityForResult(intent, REQUEST_CAMERA);
                        bottomDialog.dismiss();
                    }
                });
                View exit = v.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog.dismiss();
                    }
                });
                View imgcontent = v.findViewById(R.id.imgcontent);
                imgcontent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        intent.setType("image/*");
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, REQUEST_IMAGECOUNT);
                        bottomDialog.dismiss();
                    }
                });
            }
        });
        bottomDialog.setLayoutRes(R.layout.dialog_uploadimg);
        bottomDialog.setDimAmount(0.5f);
        bottomDialog.setCancelOutside(true);
        bottomDialog.setTag("BottomDialog");


        bottomDialog1 = BottomDialog.create(getSupportFragmentManager());
        bottomDialog1.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog1.dismiss();
                    }
                });
                View camera = v.findViewById(R.id.camera);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        temp_cmera = new File(getExternalCacheDir().getAbsolutePath()+"/" + UUID.randomUUID().toString().substring(0, 10) + ".jpg");
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra("return-data", true);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                        } else {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                        }
                        startActivityForResult(intent, REQUEST_CAMERA1);
                        bottomDialog1.dismiss();
                    }
                });
                View exit = v.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog1.dismiss();
                    }
                });
                View imgcontent = v.findViewById(R.id.imgcontent);
                imgcontent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        intent.setType("image/*");
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, REQUEST_IMAGECOUNT1);
                        bottomDialog1.dismiss();
                    }
                });
            }
        });
        bottomDialog1.setLayoutRes(R.layout.dialog_uploadimg);
        bottomDialog1.setDimAmount(0.5f);
        bottomDialog1.setCancelOutside(true);
        bottomDialog1.setTag("BottomDialog");




        bottomDialog2 = BottomDialog.create(getSupportFragmentManager());
        bottomDialog2.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog2.dismiss();
                    }
                });
                View camera = v.findViewById(R.id.camera);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        temp_cmera = new File(getExternalCacheDir().getAbsolutePath()+"/" + UUID.randomUUID().toString().substring(0, 10) + ".jpg");
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra("return-data", true);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                        } else {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                        }
                        startActivityForResult(intent, REQUEST_CAMERA2);
                        bottomDialog2.dismiss();
                    }
                });
                View exit = v.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog2.dismiss();
                    }
                });
                View imgcontent = v.findViewById(R.id.imgcontent);
                imgcontent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        intent.setType("image/*");
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, REQUEST_IMAGECOUNT2);
                        bottomDialog2.dismiss();
                    }
                });
            }
        });
        bottomDialog2.setLayoutRes(R.layout.dialog_uploadimg);
        bottomDialog2.setDimAmount(0.5f);
        bottomDialog2.setCancelOutside(true);
        bottomDialog2.setTag("BottomDialog2");

        bottomDialog3 = BottomDialog.create(getSupportFragmentManager());
        bottomDialog3.setViewListener(new BottomDialog.ViewListener() {
            @Override
            public void bindView(View v) {
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog3.dismiss();
                    }
                });
                View camera = v.findViewById(R.id.camera);
                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        temp_cmera = new File(getExternalCacheDir().getAbsolutePath()+"/" + UUID.randomUUID().toString().substring(0, 10) + ".jpg");
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra("return-data", true);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
                        } else {
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                        }
                        startActivityForResult(intent, REQUEST_CAMERA3);
                        bottomDialog3.dismiss();
                    }
                });
                View exit = v.findViewById(R.id.exit);
                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        bottomDialog3.dismiss();
                    }
                });
                View imgcontent = v.findViewById(R.id.imgcontent);
                imgcontent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                        intent.setType("image/*");
                        intent.putExtra("return-data", true);
                        startActivityForResult(intent, REQUEST_IMAGECOUNT3);
                        bottomDialog3.dismiss();
                    }
                });
            }
        });
        bottomDialog3.setLayoutRes(R.layout.dialog_uploadimg);
        bottomDialog3.setDimAmount(0.5f);
        bottomDialog3.setCancelOutside(true);
        bottomDialog3.setTag("BottomDialog2");
    }


    protected void onCreate(@Nullable Bundle paramBundle) {
        super.onCreate(paramBundle);
        Tools.initStatus(this, R.color.black);
        this.loadingdialog = new Dialog(this);
        this.loadingdialog.setContentView(R.layout.dialog_loading);
        this.loadingdialog.setCanceledOnTouchOutside(false);
        this.loadingdialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.activity_bussniess_shenfenrenzhen);
        ButterKnife.bind(this);
        this.merchantsName = getIntent().getStringExtra("merchantsName");
        this.merchantsProvince = getIntent().getStringExtra("merchantsProvince");
        this.merchantsCity = getIntent().getStringExtra("merchantsCity");
        this.merchantsArea = getIntent().getStringExtra("merchantsArea");
        this.merchantsAddress = getIntent().getStringExtra("merchantsAddress");
        this.merchantsPhone = getIntent().getStringExtra("merchantsPhone");
        this.merchantsIndustry = getIntent().getStringExtra("merchantsIndustry");
        this.merchantsLongitude = getIntent().getStringExtra("merchantsLongitude");
        this.merchantsLatitude = getIntent().getStringExtra("merchantsLatitude");
        initComponent();
    }

    protected void onDestroy() {
        super.onDestroy();
        if (this.temp_cmera != null) {
            this.temp_cmera.delete();
        }
        File localFile;
        if (!TextUtils.isEmpty(this.temppath1)) {
            localFile = new File(this.temppath1);
            System.out.println("*************temppath1:" + this.temppath1);
            localFile.delete();
        }
        if (!TextUtils.isEmpty(this.temppath2)) {
            localFile = new File(this.temppath2);
            System.out.println("*************temppath2:" + this.temppath2);
            localFile.delete();
        }
    }

    @OnClick({R.id.backbt, R.id.zhengmian_bt, R.id.fanmian_bt, R.id.shouchi_bt, R.id.yinyezhizhao_bt, R.id.submitbt})
    public void onMyClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.backbt:
                finish();
                return;
            case R.id.zhengmian_bt:
                this.bottomDialog.show();
                return;
            case R.id.fanmian_bt:
                this.bottomDialog1.show();
                return;
            case R.id.shouchi_bt:
                this.bottomDialog2.show();
                return;
            case R.id.yinyezhizhao_bt:
                this.bottomDialog3.show();
                return;
            case R.id.submitbt:
                if (TextUtils.isEmpty(this.realnameEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "真实姓名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.cardEdit.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "身份证号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (this.cardEdit.getText().toString().trim().length() < 15) {
                    Toast.makeText(getApplicationContext(), "确定是否是身份证号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.temppath1)) {
                    Toast.makeText(getApplicationContext(), "请上传正面身份证照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.temppath2)) {
                    Toast.makeText(getApplicationContext(), "请上传反面身份证照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.temppath3)) {
                    Toast.makeText(getApplicationContext(), "请上传本人手持身份证照片", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(this.temppath4)) {
                    Toast.makeText(getApplicationContext(), "请上传营业执照", Toast.LENGTH_SHORT).show();
                    return;
                }
                submit(this.realnameEdit.getText().toString().trim(), this.cardEdit.getText().toString().trim(), this.temppath1, this.temppath2, this.temppath3, this.temppath4);
                break;
        }

    }

    public void submit(String merchantsRealname, String merchantsIdcard, String file1, String file2, String file3, String file4) {
        List localList = Xutils.queryAll(getApplication(), User.class);
        if ((localList == null) || (localList.size() == 0)) {
            return;
        }
        System.out.println("************************"+file1);
        System.out.println("************************"+file2);
        System.out.println("************************"+file3);
        System.out.println("************************"+file4);
        this.loadingdialog.show();
        RequestParams localRequestParams = new RequestParams(HttpUrl.addmerchant);
        localRequestParams.setCharset("UTF-8");
        localRequestParams.setMultipart(true);
        localRequestParams.addParameter("userId", ((User) localList.get(0)).getUserId() + "");
        localRequestParams.addParameter("merchantsName", this.merchantsName);
        localRequestParams.addParameter("merchantsProvince", this.merchantsProvince);
        localRequestParams.addParameter("merchantsCity", this.merchantsCity);
        localRequestParams.addParameter("merchantsArea", this.merchantsArea);
        localRequestParams.addParameter("merchantsAddress", this.merchantsAddress);
        localRequestParams.addParameter("merchantsRealname", merchantsRealname);
        localRequestParams.addParameter("merchantsIdcard", merchantsIdcard);
        localRequestParams.addParameter("merchantsPhone", this.merchantsPhone);
        localRequestParams.addParameter("merchantsIndustry", this.merchantsIndustry);
        localRequestParams.addParameter("merchantsLongitude", this.merchantsLongitude);
        localRequestParams.addParameter("merchantsLatitude", this.merchantsLatitude);
        localRequestParams.addParameter("files", new File(file1));
        localRequestParams.addParameter("files", new File(file2));
        localRequestParams.addParameter("files", new File(file3));
        localRequestParams.addParameter("files", new File(file4));
        x.http().post(localRequestParams, new CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                loadingdialog.dismiss();
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    switch (jsonObject.getInt("state")) {
                        case HttpCode.SUCCESS:
                            webUserSserver();
                            setResult(RESULT_OK);
                            finish();
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                loadingdialog.dismiss();
                Toast.makeText(BussStoreShenfenRenzhenActivity.this.getApplicationContext(), "请稍后重试", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    public void webUserSserver() {
        final List localList = Xutils.queryAll(getApplication(), User.class);
        if ((localList == null) || (localList.size() == 0)) {
            return;
        }
        final User localUser = (User) localList.get(0);
        Map<String, String> map = new HashMap<>();
        map.put("userId", localUser.getUserId() + "");
        Xutils.POST(HttpUrl.queryuserbyid, map, new CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                UserDomain domain = gson.fromJson(result, UserDomain.class);
                switch (domain.getState()) {
                    case HttpCode.SUCCESS:
                        User data = domain.getData();
                        data.setId(localUser.getId());
                        Xutils.update(getApplication(), data);
                        break;
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
                if(resultCode!=RESULT_OK){
                    return;
                }
        Uri datauri = data.getData();
        final File finalFile1 = new File(getExternalCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString().substring(0,10)+".png");;
        Intent bgcamera = new Intent("com.android.camera.action.CROP");
        switch (requestCode) {
            case REQUEST_IMAGECOUNT:
                Glide.with(this)
                        .load(datauri)
                        .asBitmap()
                        .override(500, 500)
                        .placeholder(getResources().getColor(R.color.default_lightgray))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                try {
                                    Bitmap bitmap=null;
                                    if(resource.getHeight()<500){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                    }else if(resource.getHeight()<1000){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                    }else {
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                    }
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(finalFile1));
                                    temppath1=finalFile1.getPath();
                                    zhengmianIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;

            case REQUEST_CAMERA:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    bgcamera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                    bgcamera.setDataAndType(contentUri, "image/*");
                } else {
                    bgcamera.setDataAndType(Uri.fromFile(temp_cmera), "image/*");
                }
                bgcamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                bgcamera.putExtra("return-data", false);
                bgcamera.putExtra("scale", true);
                bgcamera.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
                bgcamera.putExtra("noFaceDetection", true);
                //     bgcamera.putExtra("aspectX",son1);
                //     bgcamera.putExtra("aspectY",son1);
                bgcamera.putExtra("outputX", 500);
                bgcamera.putExtra("outputY", 500);
                startActivityForResult(bgcamera, REQUEST_CROP);
                break;

            case REQUEST_CROP:
                Glide.with(this)
                        .load(temp_cmera.getPath())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Bitmap bitmap=null;
                                if(resource.getHeight()<500){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                }else if(resource.getHeight()<1000){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                }else {
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(temp_cmera));
                                    temppath1=temp_cmera.getPath();
                                    zhengmianIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;


            case REQUEST_IMAGECOUNT1:
                Glide.with(this)
                        .load(datauri)
                        .asBitmap()
                        .override(500, 500)
                        .placeholder(getResources().getColor(R.color.default_lightgray))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                try {
                                    Bitmap bitmap=null;
                                    if(resource.getHeight()<500){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                    }else if(resource.getHeight()<1000){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                    }else {
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                    }
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(finalFile1));
                                    temppath2=finalFile1.getPath();
                                    fanmianIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;

            case REQUEST_CAMERA1:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    bgcamera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                    bgcamera.setDataAndType(contentUri, "image/*");
                } else {
                    bgcamera.setDataAndType(Uri.fromFile(temp_cmera), "image/*");
                }
                bgcamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                bgcamera.putExtra("return-data", false);
                bgcamera.putExtra("scale", true);
                bgcamera.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
                bgcamera.putExtra("noFaceDetection", true);
                //     bgcamera.putExtra("aspectX",son1);
                //     bgcamera.putExtra("aspectY",son1);
                bgcamera.putExtra("outputX", 500);
                bgcamera.putExtra("outputY", 500);
                startActivityForResult(bgcamera, REQUEST_CROP);
                break;

            case REQUEST_CROP1:
                Glide.with(this)
                        .load(temp_cmera.getPath())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Bitmap bitmap=null;
                                if(resource.getHeight()<500){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                }else if(resource.getHeight()<1000){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                }else {
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(temp_cmera));
                                    temppath2=temp_cmera.getPath();
                                    fanmianIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;
            case REQUEST_IMAGECOUNT2:
                Glide.with(this)
                        .load(datauri)
                        .asBitmap()
                        .override(500, 500)
                        .placeholder(getResources().getColor(R.color.default_lightgray))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                try {
                                    Bitmap bitmap=null;
                                    if(resource.getHeight()<500){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                    }else if(resource.getHeight()<1000){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                    }else {
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                    }
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(finalFile1));
                                    temppath3=finalFile1.getPath();
                                    shouchiIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;

            case REQUEST_CAMERA2:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    bgcamera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                    bgcamera.setDataAndType(contentUri, "image/*");
                } else {
                    bgcamera.setDataAndType(Uri.fromFile(temp_cmera), "image/*");
                }
                bgcamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                bgcamera.putExtra("return-data", false);
                bgcamera.putExtra("scale", true);
                bgcamera.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
                bgcamera.putExtra("noFaceDetection", true);
                //     bgcamera.putExtra("aspectX",son1);
                //     bgcamera.putExtra("aspectY",son1);
                bgcamera.putExtra("outputX", 500);
                bgcamera.putExtra("outputY", 500);
                startActivityForResult(bgcamera, REQUEST_CROP);
                break;

            case REQUEST_CROP2:
                Glide.with(this)
                        .load(temp_cmera.getPath())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Bitmap bitmap=null;
                                if(resource.getHeight()<500){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                }else if(resource.getHeight()<1000){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                }else {
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(temp_cmera));
                                    temppath3=temp_cmera.getPath();
                                    shouchiIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;
            case REQUEST_IMAGECOUNT3:
                Glide.with(this)
                        .load(datauri)
                        .asBitmap()
                        .override(500, 500)
                        .placeholder(getResources().getColor(R.color.default_lightgray))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                try {
                                    Bitmap bitmap=null;
                                    if(resource.getHeight()<500){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                    }else if(resource.getHeight()<1000){
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                    }else {
                                        bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                    }
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(finalFile1));
                                    temppath4=finalFile1.getPath();
                                    yinyezhizhaoIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;

            case REQUEST_CAMERA3:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    bgcamera.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri contentUri = FileProvider.getUriForFile(BussStoreShenfenRenzhenActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider",temp_cmera);
                    bgcamera.setDataAndType(contentUri, "image/*");
                } else {
                    bgcamera.setDataAndType(Uri.fromFile(temp_cmera), "image/*");
                }
                bgcamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(temp_cmera));
                bgcamera.putExtra("return-data", false);
                bgcamera.putExtra("scale", true);
                bgcamera.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
                bgcamera.putExtra("noFaceDetection", true);
                //     bgcamera.putExtra("aspectX",son1);
                //     bgcamera.putExtra("aspectY",son1);
                bgcamera.putExtra("outputX", 500);
                bgcamera.putExtra("outputY", 500);
                startActivityForResult(bgcamera, REQUEST_CROP);
                break;

            case REQUEST_CROP3:
                Glide.with(this)
                        .load(temp_cmera.getPath())
                        .asBitmap()
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                Bitmap bitmap=null;
                                if(resource.getHeight()<500){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/1,resource.getHeight()/1);
                                }else if(resource.getHeight()<1000){
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/2,resource.getHeight()/2);
                                }else {
                                    bitmap= Tools.zoomBitmap(resource,resource.getWidth()/3,resource.getHeight()/3);
                                }
                                try {
                                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(temp_cmera));
                                    temppath4=temp_cmera.getPath();
                                    yinyezhizhaoIv.setImageBitmap(bitmap);
                                } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                break;
        }

    }
}

